package com.qsl.ssyx.acl.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.acl.service.RoleService;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.model.acl.Role;
import com.qsl.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-06
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/acl/role")
//@CrossOrigin //支持跨域
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result selectPage(@ApiParam(value = "当前页") @PathVariable Integer page
            , @ApiParam(value = "每页记录数")
                             @PathVariable Integer limit, RoleQueryVo roleQueryVo) {
        Page<Role> pageParam = new Page<>(page, limit);
        IPage<Role> model = roleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(model);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result save(@RequestBody Role role) {
        boolean flag = roleService.save(role);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }

    @ApiOperation("根据Id查询")
    @PostMapping("getById/{Id}")
    public Result save(@ApiParam(value = "Id") @PathVariable Integer Id) {
        Role role = roleService.getById(Id);
        return Result.ok(role);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody Role role) {
        boolean flag = roleService.updateById(role);
        return Result.ok(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("remove/{Id}")
    public Result remove(@PathVariable Integer Id) {
        boolean flag = roleService.removeById(Id);
        return Result.ok(flag);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Integer> idList) {
        boolean flag = roleService.removeByIds(idList);
        return Result.ok(idList);
    }


}

